package polimi.disttest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class ObjectRandomizer<T> {
	
	private Random random;
	private List<T> objects;
	private PowerLaw law;
	
	public ObjectRandomizer(List<T> objects, Random random) {
		this.random = random;
		this.objects = objects;
		law = new PowerLaw(random);
	}
	
	public T nextObjectZipf() {
		return nextObjectZipf(null);
	}
	
	public T nextObjectZipf(List<T> exceptions) {
		List<T> myObject = excludeObjects(exceptions);
		return myObject.get(law.zipf(myObject.size()));
	}
	
	public T nextObjectUniform() {
		return nextObjectUniform(null);
	}
	
	public T nextObjectUniform(List<T> exceptions) {
		List<T> myObject = excludeObjects(exceptions);
		return myObject.get(random.nextInt(myObject.size()));
	}
	
	private List<T> excludeObjects(List<T> exceptions) {
		List<T> myObjects;
		
		if (exceptions!=null && exceptions.size()>0) {
			myObjects = new ArrayList<T>();
			for (T o:objects) {
				if (!exceptions.contains(o)) {
					myObjects.add(o);
				}
			}
		} else {
			myObjects = objects;
		}
		
		return myObjects;
	}
	
}
